import axios from "axios";

// 创建axios实例
const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 50000,
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    // config.headers["Content-Type"] = "application/x-www-form-urlencoded";
    // 携带token
    config.headers["Authorization"] = "Bearer " + localStorage.getItem("token");
    config.headers["x-litemall-token"] = localStorage.getItem("token");
    return config;
  },
  (error) => Promise.reject(error)
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 导出axios实例
export default request;
